﻿using Devil;
using GameToolkit;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace GameToolkit.Editor
{
    [CustomEditor(typeof(MaterialGroupOverrider)), CanEditMultipleObjects]
    public class MaterialGroupOverriderEditor : UnityEditor.Editor
    {
        internal static void DoCachedMaterialsGUI()
        {
            //EditorGUILayout.BeginVertical("helpbox");
            if (DisplayInfo(string.Format("Cached Materials ({0})", MaterialCache.CachedMaterialsCount), "Details"))
            {
                var buf = ParallelUtils.GetBuilder();
                var lst = CacheUtil.GetListCahce<IMaterialInstance>();
                MaterialCache.GetCachedMaterials(lst);
                for (int i = 0; i < lst.Count; i++)
                {
                    buf.Append(lst[i].ToString());
                    buf.Append("\n");
                }
                Debug.Log(ParallelUtils.ReturnRelease(buf));
            }
            //EditorGUILayout.EndVertical();
        }

        internal static bool DisplayInfo(string info, string action)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(info);
            var act = GUILayout.Button(action, GUILayout.Width(120));
            EditorGUILayout.EndHorizontal();
            return act;
        }

        private void OnEnable()
        {
            var objs = targets;
            if (objs != null)
            {
                var overriders = new HashSet<MaterialOverrider>();
                var lst = CacheUtil.GetListCahce<MaterialOverrider>();
                for (int i = 0; i < objs.Length; i++)
                {
                    var m = objs[i] as MaterialGroupOverrider;
                    if (m != null)
                    {
                        m.GetComponentsInChildren(lst);
                        foreach (var mo in lst)
                        {
                            overriders.Add(mo);
                        }
                    }
                }
                foreach (var mo in overriders)
                {
                    mo.OnSelectedAsPreview();
                }
            }
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            EditorGUILayout.BeginVertical("helpbox");
            var mo = (MaterialGroupOverrider)target;
            if (DisplayInfo(string.Format("regist Overriders ({0})", mo.OverrideRendererCount), "Details"))
            {
                for (int i = 0; i < mo.OverrideRendererCount; i++)
                {
                    var ov = mo.GetOverrider(i);
                    if (ov == null)
                        continue;
                    Debug.LogFormat(ov, "[{0}] {1} ({2})", i, ComponentUtil.GetRelativePath(mo.transform, ov.transform), ov.GetType().Name);
                }
            }
            if (DisplayInfo(string.Format("binding ICbufferBlocks ({0})", mo.CbufferCount), "Details"))
            {
                for (int i = 0; i < mo.CbufferCount; i++)
                {
                    var ov = mo.GetCbuffer(i) as MonoBehaviour;
                    if (ov == null)
                        continue;
                    Debug.LogFormat(ov, "[{0}] {1} ({2})", i, ComponentUtil.GetRelativePath(mo.transform, ov.transform), ov.GetType().Name);
                }
            }
            DoCachedMaterialsGUI();
            EditorGUILayout.EndVertical();
        }
    }
}
